package com.example.springbootdemo.config;

import com.example.springbootdemo.i18n.CustomLocaleResolver;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.LocaleResolver;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import org.springframework.web.servlet.i18n.LocaleChangeInterceptor;
import org.springframework.web.servlet.i18n.SessionLocaleResolver;

import java.util.Locale;

/**
 * @author lishijin
 * @Description TODO
 * @createTime 2024年01月17日 15:26:00
 */
@Configuration
public class WebMvcConfig implements WebMvcConfigurer {
    @Bean
    public LocaleResolver localeResolver() {
        SessionLocaleResolver localeResolver = new SessionLocaleResolver();
        localeResolver.setDefaultLocale(Locale.CHINA);
        return localeResolver;
    }

    @Bean
    public LocaleChangeInterceptor localeChangeInterceptor() {
        LocaleChangeInterceptor interceptor = new LocaleChangeInterceptor();
        interceptor.setParamName("lang"); // 设置语言切换参数名，默认是 "lang"
        return interceptor;
    }

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        // 将 LocaleChangeInterceptor 添加到拦截器链中
        registry.addInterceptor(localeChangeInterceptor());
    }
}
